<?php require_once('Connections/learnConn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "access_level";
  $MM_redirectLoginSuccess = "aaa";
  $MM_redirectLoginFailed = "loginF.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_learnConn, $learnConn);
  	
  $LoginRS__query=sprintf("SELECT username, password, access_level FROM login WHERE username=%s AND password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $learnConn) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'access_level');
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if($loginStrGroup == "admin") {
		$MM_redirectLoginSuccess = "admin/index.php";
	}
	else if($loginStrGroup == "guru") {
		$MM_redirectLoginSuccess = "guru/index.php";
	}
	
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link href="style/style.css" type="text/css" rel="stylesheet" />
<link href="style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>

<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu.php";
	?>
    <tr>
    	<td id="menu">
        <ul>
        	<li><a href="index.php">Home</a></li>
            <li><a href="profil.php">Profil</a></li>
        	<li><a href="fasilitas.php">Fasilitas</a></li>
            <li><a href="kegiatan.php">Kegiatan</a></li>
        	<li><a href="pengumuman.php">Pengumuman</a></li>
            <li><a href="buku_tamu.php">Buku Tamu</a></li>
        	<li><a href="nilai_jadwal.php">Nilai & Jadwal</a></li>
            <li><a href="login.php">Login</a></li>
        </ul>
        </td>
    	<td id="konten"><center>
        	<form name="login" method="POST" action="<?php echo $loginFormAction; ?>">
            	<table>
  <tr>
    <td colspan="3" align="center"><img src="gambar/login.png"  /></td>
    </tr>
  <tr>
    <td>Username</td>
    <td>:</td>
    <td><input name="username" type="text" value="" size="32" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="password" type="password" value="" size="32" /></td>
  </tr>
  <tr>
    <td colspan="3" align="right"><input type="submit" value="Login" /></td>
    </tr>
</table>

            </form>
      </center></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>